package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.SyncSysCodeItemInfoEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.config.SyncConfig;
import com.pactera.madp.cp.context.SyncStateContext;
import com.pactera.madp.cp.mapper.sync.SyncSysCodeItemInfoMapper;
import com.pactera.madp.cp.service.sync.SyncSysCodeItemInfoHisService;
import com.pactera.madp.cp.service.sync.SyncSysCodeItemInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SyncSysCodeItemInfoServiceImpl extends ServiceImpl<SyncSysCodeItemInfoMapper, SyncSysCodeItemInfoEntity> implements SyncSysCodeItemInfoService {

    private final SyncSysCodeItemInfoHisService syncSysCodeItemInfoHisService;
    private final SyncConfig syncConfig;

    @Override
    public void addYesterdayToHis() {
        log.debug("字典项目：addYesterdayToHis");
        String syncDate = DateUtil.format(new Date(), Constants.DATE_FORMAT);
        syncSysCodeItemInfoHisService.syncOneDay(syncDate);
    }

    @Override
    public void deleteYesterday() {
        log.debug("字典项目：deleteYesterday");
        this.remove(Wrappers.emptyWrapper());
    }

    @Override
    public void parse() {
        log.debug("字典项目：parse");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -1);
        String fileDate = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        String filepath = SyncUtils.buildFilePath(syncConfig.getHrPath(), Constants.FN_CODE_ITEM, fileDate);
        File newestFile = SyncUtils.getNewestFile(filepath);

        List<SyncSysCodeItemInfoEntity> entityList
                = SyncUtils.parse(newestFile, Constants.SEPARATOR_STX, SyncSysCodeItemInfoEntity.class, syncConfig.getHrCharset());

        entityList = specialHandle(entityList);

        this.saveBatch(entityList);
    }

    @Override
    public void deleteHisOneDay() {
        log.debug("字典项目：deleteHisOneDay");
        // 获取30天前的日期
        Date offsetDay = DateUtil.offsetDay(new Date(), -30);
        String date = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        syncSysCodeItemInfoHisService.deleteOneDate(date);
    }

    @Override
    public void syncBusiness() {
        log.debug("字典项目：syncBusiness");
        SyncStateContext.get().setFinalState(true);
    }

    // 过滤掉脏数据
    private List<SyncSysCodeItemInfoEntity> specialHandle(List<SyncSysCodeItemInfoEntity> itemList) {
        List<SyncSysCodeItemInfoEntity> newList = new ArrayList<>();
        itemList.forEach(item -> {
            if (StrUtil.isNotEmpty(item.getSetCode()) && StrUtil.isNotEmpty(item.getItemCode())) {
                newList.add(item);
                return;
            }
            log.error("字典项目脏数据：" + JSONUtil.toJsonStr(item));
        });
        return newList;
    }

}